<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
             label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <!--        <el-input v-model="dataForm.content" placeholder=""></el-input>-->
        <quill-editor ref="text" v-model="dataForm.content" class="myQuillEditor" :options="editorOption"/>
      </el-form-item>
      <!-- <el-form-item label="0正常 1已经删除" prop="isDelected">
         <el-input v-model="dataForm.isDelected" placeholder="0正常 1已经删除"></el-input>
       </el-form-item>-->
      <el-form-item label="栏目" prop="categoryId">
        <!--      <el-input v-model="dataForm.categoryId" placeholder="栏目id"></el-input>-->
        <el-tree
          ref="tree1"
          node-key="id"
          :props="treeProps"
          show-checkbox
          :data="treeData"
          @check-change="handleClick" @node-click="nodeClick"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
  import {quillEditor} from 'vue-quill-editor'
  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  import 'quill/dist/quill.bubble.css'

  export default {
    components: {
      quillEditor
    },
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: '',
          isDelected: '',
          categoryId: ''
        },
        treeProps: {
          id: "id",
          label: 'name',
          children: 'children'
        },
        treeData: [],
        editorOption: {},
        dataRule: {
          title: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          isDelected: [
            {required: true, message: '0正常 1已经删除不能为空', trigger: 'blur'}
          ],
          categoryId: [
            {required: true, message: '栏目id不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    mounted() {
      this.ajaxTreeData()
    },
    methods: {
      handleClick(data, checked, node) {
        if (checked == true) {
          this.checkedId = data.id;
          this.$refs.tree1.setCheckedNodes([data]);
          this.dataForm.categoryId = data.id
        }
      },
      nodeClick(data, checked, node) {
        this.checkedId = data.id
        this.$refs.tree1.setCheckedNodes([data]);
      },
      ajaxTreeData(parentId = 0) {
        this.$http({
          url: this.$http.adornUrl('/wiki/category/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 0,
            'limit': 999,
            'key': '',
            parentId: parentId || 0
          })
        }).then(({data}) => {
          this.treeData = data.data.list
        })
      },
      init(id,categoryId) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/wiki/article/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.article.title
                this.dataForm.content = data.article.content
                this.dataForm.isDelected = data.article.isDelected
                //如果已经存在 就不加载了
                if (!this.dataForm.categoryId) {
                  this.dataForm.categoryId = data.article.categoryId
                }

                this.$nextTick(() => {
                  this.$refs['tree1'].setCheckedKeys([this.dataForm.categoryId])
                })
              }
            })
          } else {
            //如果在父类的parent已经选中了 categoryId  初始化tree的时候初始化
            if (categoryId) {
              this.$nextTick(() => {
                this.$refs['tree1'].setCheckedKeys([categoryId])
                this.dataForm.categoryId=categoryId
              })
            }
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wiki/article/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'isDelected': this.dataForm.isDelected,
                'categoryId': this.dataForm.categoryId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
